home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / lang / C / engl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  4.2 KB  |  171 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #if __STDC__ == 1
  3. #include <stdarg.h>
  4. #include <stdlib.h>
  5. #else
  6. #include <malloc.h>
  7. #include <varargs.h>
  8. #endif
  9. #include "stdpccts.h"
  10.  
  11. static void decodeType();
  12.  
  13. void show(tree)
  14. AST *tree;
  15. {
  16.     if ( tree == NULL ) return;
  17.     if ( tree->nodeType == ENode ) {
  18.         printf(" %s", zztokens[tree->data.e.token]);
  19.     } else {
  20.         english( tree );
  21.     }
  22. }
  23.  
  24. void before() { printf(" ("); }
  25. void after()  { printf(" )\n"); fflush(stdout); }
  26.  
  27. english(tree)
  28. AST *tree;
  29. {
  30.     extern int PrintEngl;
  31.  
  32.     if ( !PrintEngl ) return;
  33.     if ( tree==NULL ) return;
  34.     if ( tree->nodeType == SymQ )
  35.     {
  36.         printf("%s is", tree->data.s.name);
  37.         if ( (bottom(tree)->data.t.sc&scTypedef)!=0 )
  38.             printf(" equivalent to");
  39.         else
  40.         {
  41.             if ( (bottom(tree)->data.t.sc&scExtern)!=0 )
  42.                 printf(" globally visible and defined to be");
  43.         }
  44.         engl(tree->down, 0);
  45.         if ( tree->data.s.init!=NULL ) {
  46.             printf(" initialized to");
  47.             doExpr(tree->data.s.init);
  48.         }
  49.         printf("\n\n");
  50.         return;
  51.     }
  52.     printf("type is");
  53.     engl(tree, 0);
  54.     printf("\n\n");
  55. }
  56.  
  57. static void
  58. decodeType(base, plural)
  59. qBaseType *base;
  60. int plural;
  61. {
  62.     if ( base->sc&scAuto )    printf(" auto");
  63.     if ( base->sc&scRegister )printf(" register");
  64.     if ( base->sc&scStatic )    printf(" static");
  65.     if ( base->sc&scExtern )    ;
  66.     if ( base->sc&scTypedef )    ;
  67.  
  68.     if ( base->cv&cvConst )    printf(plural?" constant":" a constant");
  69.     if ( base->cv&cvVolatile )printf(plural?" volatile":" a volatile");
  70.  
  71.     if ( base->type&tSigned )    printf(" signed");
  72.     if ( base->type&tUnsigned)printf(" unsigned");
  73.     if ( base->type&tShort )    printf(" short");
  74.     if ( base->type&tLong )    printf(" long");
  75.     if ( base->type&tDouble )    printf(plural?" doubles":" double");
  76.     if ( base->type&tInt )     printf(plural?" ints":" int");
  77.     if ( base->type&tFloat )    printf(plural?" floats":" float");
  78.     if ( base->type&tChar )    printf(plural?" chars":" char");
  79.     if ( base->type&tVoid )    printf(plural?" objects of unspecified type":
  80.                                           " an object of unspecified type");
  81.     if ( base->type&tEllipsis)printf(" ...");
  82.     if ( base->type&tUnion )    printf(" union");
  83.     if ( base->type&tStruct )    printf(plural?" structs":" a struct");
  84.     if ( base->type&tEnum )    printf(" an enumerated type");
  85.     if ( base->type&tTypeName) printf(" a type called %s", base->name);
  86. }
  87.  
  88. engl(tree, plural)
  89. AST *tree;
  90. int plural;
  91. {
  92.     AST *a;
  93.  
  94.     if ( tree == NULL ) return;
  95.     switch ( tree->nodeType )
  96.     {
  97.         case SymQ :
  98.             printf(" symbol %s is", tree->data.s.name);
  99.             break;
  100.         case BaseTypeQ :
  101.             decodeType(&(tree->data.t), plural);
  102.             if ( (tree->data.t.type&tStruct)!=0 ||
  103.                  (tree->data.t.type&tEnum)!=0 ||
  104.                  (tree->data.t.type&tUnion)!=0 )
  105.             {
  106.                 if ( tree->data.t.name!=NULL )
  107.                     printf(" called %s",tree->data.t.name);
  108.                 if ( tree->right != NULL ) printf(" containing\n");
  109.                 for (a=tree->right; a!=NULL; a=a->right)
  110.                     {engl(a,0); putchar('\n');}
  111.             }
  112.             break;
  113.         case PointerQ :
  114.             if ( tree->data.p.cv&cvConst )
  115.                 printf(plural?" pointers to constant objects defined as":
  116.                               " a pointer to a constant object defined as");
  117.             else if ( tree->data.p.cv&cvVolatile )
  118.                 printf(plural?" pointers to volatile objects defined as":
  119.                               " a pointer to a volatile object defined as");
  120.             else
  121.                 printf(plural?" pointers to":" a pointer to");
  122.             break;
  123.         case ArrayQ :
  124.             if ( tree->data.a.dim == NULL ) {
  125.                 if (plural) printf(" undimensioned arrays of");
  126.                 else printf(" an undimensioned array of");
  127.             }
  128.             else {
  129.                 putchar(' ');
  130.                 if ( !plural ) printf("a ");
  131.                 doExpr(tree->data.a.dim);
  132.                 if (plural) printf("-element arrays of");
  133.                 else printf("-element array of");
  134.             }
  135.             plural = 1;
  136.             break;
  137.         case FunctionQ :
  138.             if ( tree->right!=NULL )
  139.             {
  140.                 printf(plural?" functions with args\n":
  141.                               " a function with args\n");
  142.                 for (a=tree->right; a!=NULL; a=a->right)
  143.                     {engl(a, 0); putchar('\n');}
  144.                 printf(" returning");
  145.             }
  146.             else
  147.                 printf(plural?" functions returning": " a function returning");
  148.             break;
  149.         case FieldQ :
  150.             printf(" field %s which is", tree->data.fi.name);
  151.             break;
  152.         case ENode :
  153.             doExpr(tree);
  154.         default :
  155.             fprintf(stderr, "illegal AST node type\n");
  156.             break;
  157.     }
  158.     engl(tree->down, plural);
  159. }
  160.  
  161. doExpr(tree)
  162. AST *tree;
  163. {
  164.     if ( tree == NULL ) return;
  165.     if ( tree->nodeType == ENode ) {
  166.         zzpre_ast(tree, show, before, after);
  167.     } else {
  168.         english( tree );
  169.     }
  170. }
  171.